package demo1209.stu;

import java.util.Scanner;

public class Demo_01 {
    public static void main(String[] args) {
        Student[] arr = new Student[10];
        Scanner scan = new Scanner(System.in);
        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作 1.添加学生信息\t2.查询学生信息\t" +
                    "3.修改学生信息\t4.删除学生信息\t0.退出");
            System.out.print("请输入你的操作:");
            String num = scan.next();
            switch (num) {
                case "1":
                    //添加学生信息 alt +enter
                    add(arr);
                    break;
                case "2":
//                    查询学生信息
                    findAll(arr);
                    break;
                case "3":
                    modify(arr);
//                    修改学生信息
                    break;
                case "4":
//                    删除学生信息
                    delete(arr);
                    break;
                case "0":
                    System.out.println("退出程序");
                    return;//结束当前的方法
                default:
                    System.out.println("操作错误,请重新输入");
            }

        }
    }

    private static void delete(Student[] arr) {
        System.out.println("请输入要删除的学生学号:");
        Scanner scan = new Scanner(System.in);
        int stuid = scan.nextInt();

        int index = find(arr,stuid);

        if(index == -1 ){
            System.out.println("删除失败");
            return;
        }
        arr[index] = null;
        for (int i = index; i <arr.length-1; i++) {

            if(arr[i+1]!=null){
                arr[i]= arr[i+1];
                arr[i+1] = null;

            }

        }
        System.out.println("删除成功");



    }

    private static void modify(Student[] arr) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入要修改的学生学号:");
        int stuid = scan.nextInt();

        for (int i = 0; i < arr.length; i++) {

            if (arr[i] != null && arr[i].getStuId() == stuid) {
                System.out.println("请输入修改后的学生的名字:");
                String name = scan.next();
                arr[i].setName(name);

                System.out.println("请输入修改后的学生的性别");
                ;
                String gender = scan.next();
                arr[i].setGender(gender);

                return;
            }

        }
        System.out.println("修改失败!");
        return;
    }

    private static void findAll(Student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                arr[i].show();
            }
        }
    }

    private static void add(Student[] arr) {
        System.out.println("请输入要添加的学生的学号:");
        Scanner scan = new Scanner(System.in);
        int stuId = scan.nextInt();
        System.out.println("请输入姓名:");
        String name = scan.next();
        System.out.println("请输入性别:");
        String gender = scan.next();

        Student s = new Student(stuId, name, gender);

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == null) {
                arr[i] = s;
                break;
            }

        }

    }

    public static int find(Student[] arr, int stuId) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null && arr[i].getStuId() == stuId) {
                return i;
            }
        }
        return -1;
    }
}
